package com.sbl.bajw.pc.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sbl.bajw.common.MyBatisDao;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.mapper.TermpostDao;
import com.sbl.bajw.pc.model.PolicePositionModel;
import com.sbl.bajw.pc.service.PolicePositionService;
import com.sbl.bajw.util.JsonUtil;

@Controller
@RequestMapping("/PolicePosition")
public class PolicePositionAction {
	
	@Resource
	private PolicePositionService service;
	
	@Resource
	MyBatisDao dao;
	
	@RequestMapping(value="/Edit")
	public ModelAndView Edit(HttpServletRequest request,HttpServletResponse response){
		String no = request.getParameter("no");
		PolicePositionModel model = service.getModelNo(no);
		request.setAttribute("model", model);
		ModelAndView mav = new ModelAndView();
		return mav;
	}
	
	@RequestMapping(value="/getPositionsByOrgs")
	public @ResponseBody String getPositionsByOrgs(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String orgs = request.getParameter("orgs");
		String mc = request.getParameter("mc");
		String dwnumsStr = request.getParameter("dwnum");
		String[] dwnums = null;
		if(dwnumsStr != null && !dwnumsStr.equals("")){
			dwnums = dwnumsStr.split(",");
		}
		HashMap<String, Object> parm = new HashMap<String, Object>();
		if(orgs != null && !orgs.equals("")){
			parm.put("ID", orgs+"%");
		}
		parm.put("MC", mc);
		parm.put("dwnum", dwnums);
		List<PolicePositionModel> models = service.getPositionByOrgIds(parm);
		
		String result = JsonUtil.ConvertArrayToJson(models);
		return result;
	}
	
	@RequestMapping(value="/getModel")
	public @ResponseBody String getModel(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String no = request.getParameter("no");
		PolicePositionModel model = service.getModelNo(no);
		
		String result = JsonUtil.ConvertObjectToJson(model);
		return result;
	}
	
	@RequestMapping(value="/getCenterCount")
	public @ResponseBody String getCenterCount(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String pid = request.getParameter("id");
		String level = request.getParameter("level");
		String dwnumsStr = request.getParameter("dwnum");
		String[] dwnums = null;
		if(dwnumsStr != null && !dwnumsStr.equals("")){
			dwnums = dwnumsStr.split(",");
		}
		HashMap<String, Object> parm = new HashMap<String, Object>();
		if(pid != null && !pid.equals("")){
			parm.put("id", pid+"%");
		}
		parm.put("dwnum", dwnums);
		parm.put("level", level);
		List<HashMap<String, String>> results = service.getCenterCount(parm);
		
		String result =  JsonUtil.ConvertArrayToJson(results);
		return result;
	}
	
	@RequestMapping(value="/StatisticsByOrgid")
	public @ResponseBody String StatisticsByOrgid(@RequestParam(value="orgid",required=false) String orgid) throws Exception{
		HashMap<String, Object> parm = new HashMap<String, Object>();
		if(orgid != null && !orgid.equals("")){
			parm.put("orgid", orgid+"%");
		}
		List<HashMap<String, String>> results = service.StatisticsByOrgid(parm);
		
		String result =  JsonUtil.ConvertArrayToJson(results);
		return result;
	}
	
	@RequestMapping(value="/getPoition")
	public @ResponseBody String getPoition() throws Exception{
		List<HashMap<String, Object>> models = service.getPoition(null);
		String result = JsonUtil.ConvertArrayToJson(models);
		return result;
	} 
	
	@RequestMapping(value="/getHistoryPoition")
	public @ResponseBody String getHistoryPoition(@RequestParam(value="no",required=false) String no,
			@RequestParam(value="sTime",required=false) Date sTime,
			@RequestParam(value="eTime",required=false) Date eTime) throws Exception{
		HashMap<String, Object> parm = new HashMap<String, Object>();
		parm.put("no", no);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			parm.put("sTime", sf.format(sTime));
			parm.put("eTime", sf.format(eTime));
		} catch (Exception e) {
			throw new MessageException("时间格式不正确。");
		}
		
		List<HashMap<String, Object>> models = service.getHistoryPoition(parm);
		String result = JsonUtil.ConvertArrayToJson(models);
		return result;
	} 
	
	@RequestMapping(value="/getCurrentPoition")
	public @ResponseBody String getCurrentPoition() throws Exception{
		List<HashMap<String, Object>> models = service.getPoition(null);
		String result = JsonUtil.ConvertArrayToJson(models);
		return result;
	}
	
	@RequestMapping(value="/test")
	public @ResponseBody String test() throws Exception{
		HashMap<String, Object> parm = new HashMap<String, Object>();
		String result = dao.selectPage(TermpostDao.class, parm, "listPage");
		
		return result;
	} 
}
